import java.util.*;

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */

class Solution {

    public String get(int num, int n, int k){
        StringBuilder str = new StringBuilder(num + "");
        int t = n / 2;
        int x = n % 2 == 0 ? str.length() - 1 : str.length() - 2;
        for(int i = 0; i < t; i++){
            str.append(str.charAt(x - i));
        }
        return str.toString();
    }
    public long countGoodIntegers(int n, int k) {
        long[] getn = new long[10];
        getn[0] = 1;
        for(int i = 1; i < 10; i++){
            getn[i] = getn[i - 1] * i;
        }
        long res = 0;
        Set<String> set = new HashSet();
        int t = (n + 1) / 2;
        int start = 1;
        int end = 1;
        for(int i = 0; i < t; i++){
            if(i < t - 1) start *= 10;
            end *= 10;
        }
        for(int i = start; i < end; i++){
            int x = Integer.parseInt(get(i, n, k));
            if(x % k != 0) continue;
            int num = x;
            int[] nums = new int[10];
            while(num != 0){
                nums[num % 10]++;
                num /= 10;
            }
            char[] chars = (x + "").toCharArray();
            Arrays.sort(chars);
            String str = new String(chars);
            if(set.contains(str)) continue;
            set.add(str);
            long low = 1, high = (n - nums[0]);
            for(int j = 0; j < 10; j++){
                low *= getn[nums[j]];
            }
            high *= getn[n - 1];
            res = res + high / low;
        }
        return res;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.countGoodIntegers(5, 6));
    }
}